//一般直接写在一个js文件中
layui.use(['layer', 'form'], function(){
    var layer = layui.layer
        ,form = layui.form;





    form.on('switch(filter)', function(data){
        console.log(data.elem); //得到checkbox原始DOM对象
        console.log(data.elem.checked); //开关是否开启，true或者false
        console.log(data.value); //开关value值，也可以通过data.elem.value得到
        console.log(data.othis); //得到美化后的DOM对象

        updateSwitchStatus(data.elem.id,data.elem.name,data.elem.checked ? 1 : 0)
    });


//列表页面，用于各种异步操作的
//列表页面 ，控制排序的加减号,绑定按钮。
var bindList = ["bind-markorder","bind-listorder","bind-termorder"]
var data_id = "data-id"
bindList.forEach(function (val){
    let ainame = 'ai_' + val.replace('bind-','')
    $('.'+val+' .btn-add').click(function (){
        let id = $(this).next().attr(data_id);
        let sum = Number($(this).next().val()) + 1
        $(this).next().val(sum)
        updateSort(id,ainame,sum)
    })

    $('.'+val+' .btn-subtract').click(function (){
        let id = $(this).prev().attr(data_id);
        let sum = Number($(this).prev().val())
        if(sum > 0){
            $(this).prev().val(sum - 1)
            updateSort(id,ainame,sum - 1)
        }
    })

    $('.'+val+' input').bind("input propertychange",function(event){
        let id = $(this).attr(data_id);
        let sum = Number($(this).val())
        updateSort(id,ainame,sum)
    });
})




const updateUrlList = [
    {name:'detail-list.php',url:'../controller/update_col.php'},
    {name:'term-list.php',url:'../controller/update_term_col.php'}
];
//获取当前页面路径最后的 term-list.php  例： /controller/detail/term-list.php
var pathname = window.location.pathname.split("/").pop();












//更新排序
function updateSort(id,ainame,sum){
    if(sum === ''){
        return false
    }
    let param = {'nid':id,'ai_name': ainame,'sum': sum}
    var loading2 = layer.open({ type: 3, shade: [0.25, '#000'], icon: 2, speed: 0});

    let url = "";
    for (let key in updateUrlList){
        if(updateUrlList[key].name == pathname){
            url = updateUrlList[key].url
        }
    }

    $.get(url,param,function(data,status){
        layer.close(loading2);
    });
}


//更新状态的 switch按钮。分类和详情的都放在一齐。
function updateSwitchStatus(id,name,status){
    let detailListPath = [
        { key: 'publish', path: '../controller/update_publish_status.php'},
        { key: 'recommend', path: '../controller/update_recommend_status.php'},
        { key: 'mark', path: '../controller/update_mark_status.php'},
        { key: 'term_publish', path: '../controller/update_term_publish_status.php'},
        { key: 'term_recommend', path: '../controller/update_term_recommend_status.php'},
        { key: 'term_mark', path: '../controller/update_term_mark_status.php'},
    ]
    let path = '';
    detailListPath.forEach(function (value){
        if(value.key === name){
            path = value.path
        }
    })
    //开启遮罩    常用类型。。。
    // var loading1 = layer.msg('正在加载', { icon: 16, shade: 0.3, time: 0 });
    var loading2 = layer.open({ type: 3, shade: [0.25, '#000'], icon: 2, speed: 0});

    let param = {'nid':id,'status': status}
    $.get(path,param,function(data,status){
        //关闭遮罩
        // layer.close(loading1);
        layer.close(loading2);
    });
}



});